import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/model/club/club_avatar_model.dart';
import 'package:flutter_changdian_football/widget/dialog/switch_club_dialog.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:get/route_manager.dart';

// 推荐头像
class FeaturedAvatarsDialog {

  static void show(
    BuildContext mContext, {
      Function? confirmTap,
      List<DefaultClubAvatarModel>? defaultClubAvatarList
    }
  ) {
    showDialog(
      // barrierDismissible: false,
      context: mContext,
      builder: (BuildContext context) {
        return ShowDialogPage(
          confirmTap: confirmTap,
          defaultClubAvatarList: defaultClubAvatarList
        );
      }
    );
  }

}


class ShowDialogPage extends StatefulWidget {
  const ShowDialogPage({super.key, this.confirmTap, this.defaultClubAvatarList});

  // 确认
  final Function? confirmTap;
  final List<DefaultClubAvatarModel>? defaultClubAvatarList;

  @override
  State<ShowDialogPage> createState() => _ShowDialogPageState();
}

class _ShowDialogPageState extends State<ShowDialogPage> {


// class ShowDialogPage extends Dialog {
//   ShowDialogPage({
//     this.confirmTap,
//     this.defaultClubAvatarList
//   });

  // // 确认
  // final Function? confirmTap;
  // final List<DefaultClubAvatarModel>? defaultClubAvatarList;

  int selectIndex = 0;

  @override
  Widget build(BuildContext dContext) {
    return Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      //自定义dialog布局
      child: Center(
        child: RichText(
          text: TextSpan(
            children: [
              WidgetSpan(
                child: Container(
                  margin: EdgeInsets.symmetric(horizontal: 40.w),
                  padding: EdgeInsets.only(top: 30.w, left: 20.w, right: 20.w, bottom: 10.w),
                  width: double.infinity,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12.r)
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        constraints: BoxConstraints(
                          maxHeight: 350.w
                        ),
                        child: GridView.builder(
                          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 3,
                            crossAxisSpacing: 16,
                            mainAxisSpacing: 16
                          ),
                          physics: const BouncingScrollPhysics(),
                          itemCount: widget.defaultClubAvatarList?.length ?? 0,
                          shrinkWrap: true,
                          itemBuilder: (BuildContext context, int index) {
                            return GestureDetector(
                              behavior: HitTestBehavior.translucent,
                              onTap: () {
                                setState(() {
                                  selectIndex = index;
                                });
                              },
                              child: Container(
                                width: 88.w,
                                height: 88.w,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(16.r),
                                  border: Border.all(
                                    width: 1.w,
                                    color: index == selectIndex ? const Color(0xff499360) : Colors.white
                                  )
                                ),
                                child: ClipRRect(
                                  borderRadius: BorderRadius.circular(16.r),
                                  child: Image.network(widget.defaultClubAvatarList?[index].url ?? '', fit: BoxFit.fill,),
                                )
                              ),
                            );
                          },
                        ),
                      ),

                      SizedBox(height: 38.w,),

                      GestureDetector(
                        behavior: HitTestBehavior.translucent,
                        onTap: () {
                          widget.confirmTap?.call(widget.defaultClubAvatarList?[selectIndex].url);
                        },
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 12.w),
                          decoration: BoxDecoration(
                            color: const Color(0xff499360),
                            borderRadius: BorderRadius.circular(8.r)
                          ),
                          width: double.infinity,
                          alignment: Alignment.center,
                          child: Text('确认', style: TextStyle(fontSize: 14.sp, color: Colors.white, fontWeight: FontWeight.w500),),
                        ),
                      ),
                    ],
                  ),
                )
              ),
            ]
          ),
        ),
      )
    );
  }
}

